package com.example.controller;


import com.example.entity.Student;
import com.example.entity.User;
import com.example.service.TestService;
import com.example.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller
public class MainController {

    @Value("${test.name}")
    String name;

    @Resource
    UserService userService;

    @Resource
    TestService testService;

    @RequestMapping("/")
    public String index(){

        return "index";

    }

    @RequestMapping("/stu")
    @ResponseBody
    public Student student(){
        Student student = new Student();
        student.setId(1);
        student.setName("小明");
        student.setSex("男");
        return student;
    }

    @RequestMapping("/user")
    @ResponseBody
    public User user(){
        User user = userService.findOneUser(1);
        return user;

    }


    @RequestMapping("/login")
    public String login(HttpServletRequest request){
        //session代替ID，日志添加用户session信息
        MDC.put("reqId", request.getSession().getId());
        Logger logger = LoggerFactory.getLogger(MainController.class);
        logger.info("访问登录页");
        testService.test();
        System.out.println("我是同步任务！");
        return "login";
    }


}
